﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using tricogol.app.Data;

namespace tricogol.app.Controllers
{
    public class EtapaController : ApiController
    {
        private hiugaEntities db = new hiugaEntities();

        // GET api/Etapa
        public IEnumerable<cn_etapa> Getcn_etapa()
        {
            //Error: var cn_etapa = db.cn_etapa.Include(c => c.cn_campeonato);
            return db.cn_etapa.AsEnumerable();
        }

        // GET api/Etapa/5
        public cn_etapa Getcn_etapa(int id)
        {
            cn_etapa cn_etapa = db.cn_etapa.Find(id);
            if (cn_etapa == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            return cn_etapa;
        }

        // PUT api/Etapa/5
        public HttpResponseMessage Putcn_etapa(int id, cn_etapa cn_etapa)
        {
            if (ModelState.IsValid && id == cn_etapa.id)
            {
                db.Entry(cn_etapa).State = EntityState.Modified;

                try
                {
                    db.SaveChanges();
                }
                catch (DbUpdateConcurrencyException)
                {
                    return Request.CreateResponse(HttpStatusCode.NotFound);
                }

                return Request.CreateResponse(HttpStatusCode.OK);
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

        // POST api/Etapa
        public HttpResponseMessage Postcn_etapa(cn_etapa cn_etapa)
        {
            if (ModelState.IsValid)
            {
                db.cn_etapa.Add(cn_etapa);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, cn_etapa);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = cn_etapa.id }));
                return response;
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

        // DELETE api/Etapa/5
        public HttpResponseMessage Deletecn_etapa(int id)
        {
            cn_etapa cn_etapa = db.cn_etapa.Find(id);
            if (cn_etapa == null)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            db.cn_etapa.Remove(cn_etapa);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            return Request.CreateResponse(HttpStatusCode.OK, cn_etapa);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}